package com.whfc.common.third.whxph;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.whfc.common.third.whxph.entity.WaterQualityItem;
import com.whfc.common.util.HttpUtil;
import com.whfc.common.util.JSONUtil;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.util.Collections;
import java.util.List;

/**
 * @Description: 水质监测API
 * @author: xugcheng
 * @version: 1.0
 * @date: 2021-03-09 9:25
 */
public class WaterQualityApi {

    private static Logger logger = LoggerFactory.getLogger(WaterQualityApi.class);

    private static final String DATA_URL = "http://47.105.215.208:8005/intfa/queryData/%s";

    private static final String SUCCESS = "200";

    /**
     * 查询水质数据
     *
     * @param deviceCode
     * @return
     */
    public static List<WaterQualityItem> queryData(String deviceCode) {
        String url = String.format(DATA_URL, deviceCode);
        String response = HttpUtil.doGet(url);
        logger.info("获取水质监测数据,{}", response);
        JSONObject jsonObject = JSONObject.parseObject(response);
        if (jsonObject != null) {
            String statusCode = jsonObject.getString("statusCode");
            if (SUCCESS.equals(statusCode)) {
                JSONArray jsonArray = jsonObject.getJSONArray("entity");
                List<WaterQualityItem> itemList = JSON.parseArray(jsonArray.toJSONString(), WaterQualityItem.class);
                return itemList;
            }
        }
        return Collections.emptyList();
    }

    public static void main(String[] args) {
        List<WaterQualityItem> itemList = WaterQualityApi.queryData("16074360");
        System.out.println(JSONUtil.toPrettyString(itemList));
    }
}
